#include "MainMenu.h"

#include <SFML/Window/Event.hpp>
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Mouse.hpp>

#include "GameApplication.h"
#include "GameView.h"
#include "GameLocation.h"

using namespace sf;
using namespace mega;

MainMenu::MainMenu()
	: Menu()
{
	m_shape = RectangleShape(Vector2f(300, 600));
	m_shape.setFillColor(Color(Uint8(128), Uint8(0), Uint8(0)));

	// create buttons
	m_items.push_back(new MenuItem(RectangleShape(Vector2f(200, 40))));
	m_items.back()->setText("NEW GAME");

	m_items.push_back(new MenuItem(RectangleShape(Vector2f(200, 40))));
	m_items.back()->setText("EXIT");

	// init items properties and expand
	initItems();
	expand();

	this->move(Vector2f(300, 100));
}


MainMenu::~MainMenu()
{}


bool MainMenu::onKeyReleased(const Keyboard::Key & _key)
{
	bool isHandled = false;

	if (Keyboard::N == _key)
	{
		gApp().pushView(new GameLocation);
		isHandled = true;
	}

	return isHandled;
}


bool MainMenu::onMouseReleased(const Mouse::Button & _button, const Position & _pos)
{
	bool isHandled = false;

	if (Mouse::Left == _button)
	{
		Position mousePos = gApp().window().mapPixelToCoords(Mouse::getPosition());

		// new game menu item clicked
		if (m_items.at(0)->getGlobalBounds().contains(mousePos))
		{
			gApp().pushView(new GameLocation);
			isHandled = true;
		}

		// exit menu item clicked
		else if (m_items.at(1)->getGlobalBounds().contains(mousePos))
		{
			gApp().popView();
			isHandled = true;
		}
	}

	return isHandled;
}
